/*
 * strcliselect02.c
 *
 *  Created on: Apr 9, 2012
 *      Author: root
 */

#include	"unp.h"

void str_cli(FILE *fp, int sockfd) {
	int maxfdp1, stdineof, toggle = 0;
	fd_set rset;
	char buf[MAXLINE], buf1[MAXLINE];
	char message[MAXLINE];
	char fromme[MAXLINE];
	int n;
	char *command;
	char *debug_msg;
	char myid[MAXLINE], youid[MAXLINE];
	char youid2[MAXLINE];
	char ID_list[MAXLINE];
	char who[MAXLINE];
	stdineof = 0;
	FD_ZERO(&rset);
	printf("------first you input help------\n");
	printf(">");
	fflush(stdout);
	for (;;) {

		if (stdineof == 0)
			FD_SET(fileno(fp), &rset);
		FD_SET(sockfd, &rset);
		maxfdp1 = max(fileno(fp), sockfd) + 1;
		Select(maxfdp1, &rset, NULL, NULL, NULL);
		if (FD_ISSET(sockfd, &rset)) { /* socket is readable */
			if ((n = Read(sockfd, buf, MAXLINE)) == 0) {
				if (stdineof == 1)
					return; /* normal termination */
				else
					err_quit("str_cli: server terminated prematurely");
			}
			sprintf(buf1, "%s", buf);
			debug_msg = strtok(buf, " ");

			//////////debug off don't display request, response message////////
			if (toggle == 0) {
				if (!strcmp(buf, "230")) {
					debug_msg = strtok(NULL, "\r");
					sprintf(buf, "%s\n", debug_msg);

					Write(fileno(stdout), buf, strlen(buf));
					printf(">");
					fflush(stdout);
				}

				if (!strcmp(buf, "210")) {
					debug_msg = strtok(NULL, "\r");
					sprintf(who, "%s\n", debug_msg);
					debug_msg = strtok(NULL, "\r");
					sprintf(ID_list, "%s\n", debug_msg);
					sprintf(buf, "%s%s", who, ID_list);
					Write(fileno(stdout), buf, strlen(buf));
					printf(">");
					fflush(stdout);
				}

				if (!strcmp(buf, "299")) {
					debug_msg = strtok(NULL, "\n");
					debug_msg = strtok(NULL, "\n");

					sprintf(fromme, "%s", debug_msg);

					debug_msg = strtok(NULL, "\r");

					command = strtok(fromme, " ");
					command = strtok(NULL, " ");
					sprintf(youid2, "%s", command);

					sprintf(buf, "\n%s : %s\n", youid2, debug_msg);

					Write(fileno(stdout), buf, strlen(buf));
					//printf("%s:", myid);
					printf(">");
					fflush(stdout);

				}

				if (!strcmp(buf, "221")) {
					debug_msg = strtok(NULL, "\r");
					sprintf(buf, "%s\n", debug_msg);
					Write(fileno(stdout), buf, strlen(buf));
					stdineof = 1;
					Shutdown(sockfd, SHUT_WR); /* send FIN */
					FD_CLR(fileno(fp), &rset);
					continue;
				}

				if (!strcmp(buf, "530")) {
					debug_msg = strtok(NULL, "\r");
					sprintf(buf, "%s\n", debug_msg);
					Write(fileno(stdout), buf, strlen(buf));
					printf(">");
					fflush(stdout);
				}

				if (!strcmp(buf, "500")) {
					debug_msg = strtok(NULL, "\r");
					sprintf(buf, "%s\n", debug_msg);
					Write(fileno(stdout), buf, strlen(buf));
					printf(">");
					fflush(stdout);
				}
			}

			///////////debug on , display request ,response message//////////
			if (toggle == 1) {
				if (!strcmp(buf, "221")) {
					Write(fileno(stderr), buf1, n);
					stdineof = 1;
					Shutdown(sockfd, SHUT_WR); /* send FIN */
					FD_CLR(fileno(fp), &rset);
					continue;
				} else {
					Write(fileno(stderr), buf1, n);
					printf(">");
					fflush(stdout);
				}
			}
			memset(buf, 0, strlen(buf));
			memset(message, 0, strlen(message));
			memset(buf1, 0, strlen(buf1));
		}

		if (FD_ISSET(fileno(fp), &rset)) {
			/* input is readable */
			if ((n = readline(fileno(fp), buf, MAXLINE)) == 0) {
				stdineof = 1;
				Shutdown(sockfd, SHUT_WR); /* send FIN */
				FD_CLR(fileno(fp), &rset);
				continue;
			}
			memset(buf1, 0, strlen(buf1));

			command = strtok(buf, "\n");
			sprintf(buf1, "%s\r\n", buf);

			///////////who command/////////
			if (!strcmp("who\r\n", buf1)) {
				memset(buf1, 0, strlen(buf1));
				sprintf(buf1, "WHO\r\n");

			}

			//////////quit command///////////
			if (!strcmp("quit\r\n", buf1)) {
				printf("***************\n");
				sprintf(buf1, "QUIT\r\n");

			}

			/////////help command///////////
			if (!strcmp("help\r\n", buf1)) {
				printf(
						"****************************************************\n");
				printf("----User [ID(my id)]'Enter' is Log in\n");
				printf("----who'Enter' is Logged User ID list\n");
				printf("----talk [ID(your id)]'Enter' is talk mode\n");
				printf("----In the talk mode, [message] typing\n");
				printf("----In the talk mode, .'Enter'  is End of talk mode\n");
				printf("----debug'Enter' is debug mode!\n");
				printf("----quit'Enter' is Close the chatting\n");
				printf(
						"****************************************************\n");

			}

			///////////////debug command//////////////
			if (!strcmp("debug\r\n", buf1)) {
				if (toggle == 0) {
					printf("debug on!\n");
					toggle = 1;
				} else {
					toggle = 0;
					printf("debug off!\n");
				}
			}

			if (!strcmp("USER\r\n", buf1)) // "USER" enter syntax error find
					{
				sprintf(buf1, "error");
			}

			command = strtok(buf1, " ");
			if (!strcmp("USER", buf1)) //USER command syntax error find
					{
				sprintf(buf1, "error");
			}

			///////////user command///////////////
			if (!strcmp("user", buf1)) {
				command = strtok(NULL, " \r\n");
				sprintf(myid, "%s", command);
				sprintf(buf1, "USER %s\r\n", command);
			}

			///////////talk command/////////////////
			if (!strcmp("talk", buf1)) {
				printf("----------------------talk mode------------------\n");
				command = strtok(NULL, "\r");
				sprintf(youid, "%s", command);

				printf("%s:", myid);
				fflush(stdout);
				memset(buf, 0, strlen(buf));
				memset(message, 0, strlen(message));
				memset(buf1, 0, strlen(buf1));

				for (;;) {

					if (stdineof == 0)
						FD_SET(fileno(fp), &rset);
					FD_SET(sockfd, &rset);
					maxfdp1 = max(fileno(fp), sockfd) + 1;
					Select(maxfdp1, &rset, NULL, NULL, NULL);
					if (FD_ISSET(sockfd, &rset)) { /* socket is readable */
						if ((n = Read(sockfd, buf, MAXLINE)) == 0) {
							if (stdineof == 1)
								return; /* normal termination */
							else
								err_quit(
										"str_cli: server terminated prematurely");
						}

						strcpy(buf1, buf);
						debug_msg = strtok(buf1, " ");

						////////debug off don't display request, response message
						if (toggle == 0) {
							if (!strcmp(buf1, "299")) {
								debug_msg = strtok(NULL, "\n");
								debug_msg = strtok(NULL, "\n");

								sprintf(fromme, "%s", debug_msg);

								debug_msg = strtok(NULL, "\r");

								command = strtok(fromme, " ");
								command = strtok(NULL, " ");
								sprintf(youid2, "%s", command);

								sprintf(buf1, "\n%s : %s\n", youid2, debug_msg);

								Write(fileno(stdout), buf1, strlen(buf1));
								printf("%s:", myid);
								fflush(stdout);

							}
							if (!strcmp(buf1, "200")) {
								sprintf(buf, "\n");
								Write(fileno(stdout), buf, strlen(buf));
								printf("%s:", myid);
								fflush(stdout);
							}

							if (!strcmp(buf1, "530")) {
								debug_msg = strtok(NULL, "\r");
								sprintf(buf, "%s\n", debug_msg);
								Write(fileno(stdout), buf, strlen(buf));
								printf("%s:", myid);
								fflush(stdout);
							}

							if (!strcmp(buf1, "500")) {
								debug_msg = strtok(NULL, "\r");
								sprintf(buf, "%s\n", debug_msg);
								Write(fileno(stdout), buf, strlen(buf));
								printf("%s:", myid);
								fflush(stdout);

							}

						}

						// debug on , display request ,response message
						if (toggle == 1) {

							Write(fileno(stderr), buf, n);

							printf("%s:", myid);
							fflush(stdout);

						}

						memset(buf, 0, strlen(buf));
						memset(buf1, 0, strlen(buf1));
						memset(message, 0, strlen(message));
					}

					if (FD_ISSET(fileno(fp), &rset)) {
						/* input is readable */
						if ((n = readline(fileno(fp), message, MAXLINE)) == 0) {
							stdineof = 1;
							Shutdown(sockfd, SHUT_WR); /* send FIN */
							FD_CLR(fileno(fp), &rset);
							continue;
						}

						/////////////. command///////////////////
						if (!strcmp(message, ".\n")) {
							memset(buf, 0, strlen(buf));
							memset(buf1, 0, strlen(buf1));
							memset(message, 0, strlen(message));
							printf(
									"----------------------close talk mode--------------------\n");

							break;
						}

						command = strtok(message, "\n");
						sprintf(message, "%s\r\n", message);

						sprintf(buf, "FROM %s TO %s\r\n%s\r\n", myid, youid,
								message);
						//Fputs(buf,stdout);

						if (toggle == 1) // request message
								{
							Write(fileno(stderr), buf, strlen(buf));
						}

						Writen(sockfd, buf, strlen(buf));
						memset(buf, 0, strlen(buf));
						memset(message, 0, strlen(message));
						memset(buf1, 0, strlen(buf1));
					}
				}
			}

			///////display request message/////////
			if (toggle == 1) //
					{
				if (!strcmp(buf1, "help\r\n") || !strcmp(buf1, "debug\r\n")
						|| !strcmp(buf1, ".\r\n")) {
					printf(">");
					fflush(stdout);

				} else
					Write(fileno(stderr), buf1, strlen(buf1));
			}

			////////////////send request message/////////////
			if (!strcmp(buf1, "help\r\n") || !strcmp(buf1, "debug\r\n")
					|| !strcmp(buf1, ".\r\n")) {

			} else {
				if (!strcmp(buf1, "who")) //who xxxxx syntax error find
					sprintf(buf1, "error\r\n");

				sprintf(buf1, "%s\r\n", buf1);

				Writen(sockfd, buf1, strlen(buf1));
			}

			memset(buf, 0, strlen(buf));
			memset(buf1, 0, strlen(buf1));

		}

	}
}

